package com.juejin.www.controller;

import com.juejin.www.aspect.Log;
import com.juejin.www.aspect.Perm;
import com.juejin.www.model.DTO.LikeDTO;
import com.juejin.www.model.DTO.Result;
import com.juejin.www.service.LikeService;
import com.juejin.www.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Jacky
 */
@Slf4j
@RestController
@RequestMapping("/like")
public class LikeController {

    @Autowired
    private LikeService likeService;

    @Log
    @Perm(permission = "like:add")
    @PostMapping
    public Result add(@RequestBody LikeDTO likeDTO, @RequestHeader("token") String jwt){
        Integer uid = JwtUtils.getIdByJWT(jwt);
        likeDTO.setUserId(uid);
        if(likeDTO.getBeLikedType()==1){
            log.info("{}用户点赞{}号文章",likeDTO.getUserId(),likeDTO.getBeLikedId());
        }else if(likeDTO.getBeLikedType()==2){
            log.info("{}用户点赞{}号沸点",likeDTO.getUserId(),likeDTO.getBeLikedId());
        }else {
            log.info("{}用户点赞{}号评论",likeDTO.getUserId(),likeDTO.getBeLikedId());
        }
        boolean bol = likeService.add(likeDTO);
        if(bol){
            return Result.success();
        }else {
            return Result.error("您已点赞，请勿重复点赞,请刷新网页！");
        }
    }

    @Log
    @Perm(permission = "like:delete")
    @DeleteMapping
    public Result cancelLike(@RequestBody LikeDTO likeDTO,@RequestHeader("token") String jwt){
        Integer uid = JwtUtils.getIdByJWT(jwt);
        likeDTO.setUserId(uid);
        if(likeDTO.getBeLikedType()==1){
            log.info("{}用户取消点赞{}号文章",likeDTO.getUserId(),likeDTO.getBeLikedId());
        }else if(likeDTO.getBeLikedType()==2){
            log.info("{}用户取消点赞{}号沸点",likeDTO.getUserId(),likeDTO.getBeLikedId());
        }else {
            log.info("{}用户取消点赞{}号评论",likeDTO.getUserId(),likeDTO.getBeLikedId());
        }
        likeService.cancelLike(likeDTO);
        return Result.success();
    }
}
